const prefix = 'APP_',
  TOKEN = `${prefix}TOKEN`,
  IS_LOGIN = `${prefix}LOGIN`,
  USERINFO = `${prefix}USERINFO`;

/**
 * 存储localStorage
 * @param {*} name
 * @param {*} content
 */
export const setStorage = (name, content) => {
  if (!name) return
  if (typeof content !== 'string') {
    content = JSON.stringify(content)
  }
  localStorage.setItem(name, content)
}

/**
 * 获取localStorage
 * @param name
 */
export const getStorage = (name) => {
  if (!name) return
  return localStorage.getItem(name)
}

//设置是否登录
export const setIsLogin = (isLogin = 0) => {
  setStorage(IS_LOGIN, isLogin);
}

//判断是否登录
export const isLogin = () => {
  return !!getStorage(IS_LOGIN) && !!getToken();
}

//设置token
export const setToken = token => {
  setStorage(TOKEN, token);
}

//获取token
export const getToken = () => {
  getStorage(TOKEN);
}

//设置用户信息
export const setUserInfo = userInfo => {
  setStorage(USERINFO, userInfo);
}

//获取用户信息
export const getUserInfo = () => {
  getStorage(USERINFO);
}

// 删除登录信息
export const removeToken = () => {
  localStorage.removeItem(TOKEN);
  localStorage.removeItem(IS_LOGIN);
  localStorage.removeItem(USERINFO);
}
